﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using eCommerce.core;
using eCommerce.core.AdminMenu;
using eCommerce.core.Authenticated;
using eCommerce.core.Helpers;

namespace eCommerce.cms.Controls.AdminMenu
{
    public partial class AdminMenuManager : AuthenticatedControl
    {
        protected int TotalMenu;
        protected int TotalIsVisibled;
        protected int TotalIsVisibleWhenRun;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            var ucId = Utils.GetIdFromRequestString(Utils.UserControlId, string.Empty);

            switch (ucId)
            {
                case "edit":
                    Control c1 = LoadControl("CreateOrEdit.ascx");
                    plcControl.Controls.Add(c1);
                    break;                
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeMenu();
                GetStatistics();
            }
        }

        private void GetStatistics()
        {
            TotalIsVisibled         = AdminMenuController.AdminMenu_GetStatistics(AdminMenuObject.IsVisibled);
            TotalIsVisibleWhenRun   = AdminMenuController.AdminMenu_GetStatistics(AdminMenuObject.IsVisibledWhenRun);
        }
        
        private void BindTreeMenu() 
        {
            var menus = new List<AdminMenuObject>();
            AdminMenuController.AdminMenu_GetTree(menus, 0, 0);            

            if (menus.Count > 0)
            {
                TotalMenu = menus.Count;
                lvMenu.DataSource = menus;
                lvMenu.DataBind();
            }
        }

        protected string GetStatus(int status)
        {
            return string.Format(status == 1 ? "<img src=\"/images/icons/dark/footprints.png\" alt=\"Hiển thị\" title=\"Hiển thị\" class=\"btn14 topDir\">"
                                             : "<img src=\"/images/icons/dark/walkingMan.png\" alt=\"Khi chạy mới hiển thị\" title=\"Khi chạy mới hiển thị\" class=\"btn14 topDir\">");
        }

        protected void lvMenu_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                if (string.Equals(e.CommandName, "DeleteMenu"))
                {
                    var dataItem = (ListViewDataItem)e.Item;
                    var id = Utils.ToInt32(lvMenu.DataKeys[dataItem.DisplayIndex].Value);

                    if (id > 0)
                    {
                        var menu = AdminMenuController.AdminMenu_GetById(id);

                        if (menu != null)
                        {
                            AdminMenuController.AdminMenu_Delete(id);

                            ltMessage.Text = Utils.ShowMessage("Xóa bản ghi thành công.", MessageType.Success);

                            BindTreeMenu();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                ltMessage.Text = Utils.ShowMessage(ex.Message, MessageType.Failure);
            }
        } 
    }
}